package actions;

import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;
import forms.DegreeForm;
import model.DisciplineModel;

public class DegreeUniversityAction extends Action {

	private static final boolean DEBUG = true;
	//private static final int UNKNOWN = -1;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws DbException {
	
		// get degree location from degree form
		DegreeForm dform = (DegreeForm)form;
		String university = dform.getUniversity();
		
		if (DEBUG) {
			System.err.println("DegreeUniversityAction: saving university(" + university +")");
		}
		
		// save degree location into session
		request.getSession().setAttribute("university", university);
		
		// store DegreeForm into session
		dform = (DegreeForm)request.getSession().getAttribute("dform");
		dform.setUniversity(university);
		
		// put list of disciplines into a vector
		RowSet rset = DisciplineModel.getAllDiscipline();
		
		Vector<String> disciplines = new Vector<String>();

		int count = 0;
		
		try {
			while (rset.next()) {
				disciplines.add(rset.getString("discipline"));
			}
		} catch (SQLException e) {
			System.err.println("SQLException: " + e.getMessage());
		}
		
		request.getSession().setAttribute("disciplines", disciplines);
		
		return mapping.findForward("success");
	}
	
}
